﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kakaroto.Models;
using System.Web.ModelBinding;

namespace Kakaroto.Pedidos
{
    public partial class Default : System.Web.UI.Page
    {
        string buscaText = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            Search.Focus();
        }

        public IQueryable<Pedido> GetPedidos(
                        [QueryString("id")] int? IdMesa)
        {
            var _db = new KakarotoEntities();
            PedidoActions actPedido = new PedidoActions();
            actPedido.RestauraItensDeEdicaoNaoFinalizada();

            IQueryable<Pedido> query = _db.Pedido;
            query = query.Where(p => p.Status_Pagamento == false);

            if (IdMesa.HasValue)
            {
                query = query.Where(p => p.Id_Mesa == IdMesa);
            }

            query = query.OrderByDescending(p => p.Entrada);
            return query;
        }

        protected void AlteraBtn_Click(object sender, EventArgs e)
        {
            PedidoActions actPedido = new PedidoActions();
            Button btn = (Button)sender;

            if (btn.CommandName.Equals("AltPed"))
            {
                actPedido.PreparaAtualizacao(btn.CommandArgument.ToString());
            }
            Response.Redirect("./ListaItensPedido.aspx");
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            buscaText = Search.Value;
            if (!string.IsNullOrEmpty(buscaText))
            {
                Response.Redirect("./Default.aspx?id=" + buscaText);
            }
            Response.Redirect("./Default.aspx");
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            ListaPedidosAtivos.DataBind();
        }
    }
}